package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.PlanRemindListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.PlanRemind;
import com.isunland.managesystem.entity.PlanRemindOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/4 09:55
 */
public class PlanMyRemindListFragment extends BaseListFragment {

  private ArrayList<PlanRemind> mList;
  private int mPage;
  private CurrentUser mUser;
  private PlanRemindListAdapter mAdapter;
  private int REQUEST_DETAIL = 0;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.myReminder);
    mList = new ArrayList<>();
    mUser = CurrentUser.newInstance(getActivity());
    mPage = 1;
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public String getUrl() {
    return ApiConst.URL_PLAN_MYREMIND_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mUser.getMemberCode());
    params.put("userJobNo", mUser.getJobNumber());
    params.put("pageNumber", mPage + "");
    params.put("pageSize", "20");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {

    PlanRemindOriginal original = new Gson().fromJson(json, PlanRemindOriginal.class);
    ArrayList<PlanRemind> rows = original.getRows();
    if (Base.SUCCESS == original.getResult() && rows != null) {
      if (rows.size() == 0) {
        ToastUtil.show(R.string.that_is_all);
      }
      if (mPage == 1) {
        mList.clear();
      }
      mList.addAll(rows);
      if (mAdapter == null) {
        mAdapter = new PlanRemindListAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
        return;
      }
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }

    /**测试数据*/
    //PlanRemind content=new PlanRemind();
    //content.setOncedate("2017_05-01");
    //content.setTipinfo("提醒内容");
    //content.setTiptime(new Date());
    //content.setTipname("被提醒的人: 胡晓丹");
    //content.setRemark("我的备注");
    //content.setRegStaffName("胡晓丹");
    //content.setRegDate(new Date());
    //for (int i=0;i<10;i++){
    //  mList.add(content);
    //}
    //mAdapter = new PlanRemindListAdapter(getActivity(), mList);
    //setListAdapter(mAdapter);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    PlanRemind content = mList.get(position - 1);
    Intent i = new Intent(getActivity(), PlanRemindDetailActivity.class);
    i.putExtra(PlanRemindDetailFragment.EXTRA_CONTENT, content);
    startActivityForResult(i, REQUEST_DETAIL);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    if (PullToRefreshBase.Mode.PULL_FROM_START == refreshView.getCurrentMode()) {
      mPage = 1;
    }
    if (PullToRefreshBase.Mode.PULL_FROM_END == refreshView.getCurrentMode()) {
      mPage++;
    }
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DETAIL) {
      mPage = 1;
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
      volleyPost();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    }
  }
}
